package org.mobile.makibis;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.mobile.makibis.AddSpecies.MyOnItemSelectedListener;
import org.mobile.makibis.util.ContentLoader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class EditSpecies extends Activity {

	private ArrayAdapter<String> spinnerAdapter;
	private Spinner spinner;
	private List<String> spinnerArray;
	String feedURL = "http://10.0.2.2:8080/MakiBIS/";
	ContentLoader con = new ContentLoader();
	private int speciesID;
	private JSONArray jsonarray;
	private JSONObject obj;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editspecies);
		
		try{
			JSONObject content = new org.json.JSONObject(con.loadJSON(feedURL + "retrieve/species"));
			jsonarray = content.getJSONArray("data");
			for(int i=0; i<jsonarray.length(); i++) {			
				obj = jsonarray.getJSONObject(i);
				spinnerArray.add(obj.getString("name"));
			}
			Log.d("array", "" + spinnerArray.toString());
			createSpinner();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void createSpinner(){
		spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray){
			public boolean isEnabled(int position) {
                if(position == 0){
                    return false; 
                }else{
                    return true;
                }
              } 
              public boolean areAllItemsEnabled() { 
                return false; 
              } 
		};
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerAdapter.notifyDataSetChanged();
		spinner.setAdapter(spinnerAdapter);	
		spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		spinnerAdapter.notifyDataSetChanged();
		
	}
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		      Log.d("YOU", "JASAKJSAS");
		}
	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	
	}

}
